package org.zjx.controller;


import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.media.Schema;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;
import org.zjx.common.bean.Result;
import org.zjx.common.util.BeanUtil;
import org.zjx.dao.dto.DeviceDailyTaskDTO;
import org.zjx.dao.entity.*;
import org.zjx.dao.vo.DailyTasksVO;
import org.zjx.dao.vo.DeviceDailyTaskVO;
import org.zjx.dao.vo.TaskVO;
import org.zjx.service.*;
import org.zjx.service.impl.TaskServiceImpl;

@Tag(name = "任务执行设备 管理接口")
@RestController
@RequestMapping("/dailyTask")
@RequiredArgsConstructor
public class DeviceDailyTaskController {
    private final IDailyTasksService dailyTasksService;
    private final IAccountService accountService;
    private final IRoleService roleService;
    private final IServerService serverService;
    private final IDeviceDailyTaskService deviceDailyTaskService;
    private final ISectsService sectsService;
    private final IDeviceService deviceService;
    private final ITaskService taskService;

    @Operation(summary = "上传设备及窗口 获取当前执行角色")
    @PostMapping("/queryDailyTask")
    @Transactional(rollbackFor = Exception.class)
    public Result<DeviceDailyTaskVO> queryUnusedRole(@RequestBody DeviceDailyTaskDTO deviceDailyTaskDTO) {
        Device device = deviceService.getDeviceByCard(deviceDailyTaskDTO.getCpu());
        if (device == null) {return Result.isFail(new Exception("设备没有记录"));}
        deviceDailyTaskDTO.setDeviceId(device.getId());

        DailyTasks dailyTasks = dailyTasksService.selectByConditions(deviceDailyTaskDTO.getDeviceId(),deviceDailyTaskDTO.getTemporary());
        if (dailyTasks == null) {
            dailyTasks = dailyTasksService.queryTasksOnAccountUnUsd();
            if (dailyTasks == null) return Result.isOk().data(null);
            dailyTasksService.updateByConditions(dailyTasks.getId());
            deviceDailyTaskService.updateByConditions(dailyTasks.getId(),deviceDailyTaskDTO.getDeviceId(),deviceDailyTaskDTO.getTemporary());
        }

        Role role = roleService.getById(dailyTasks.getTaskId());
        Account account = accountService.getById(role.getAccountId());
        Server server = serverService.getById(role.getServiceId());
        Sects sects = sectsService.getById(role.getSectsId());

        DeviceDailyTaskVO userVO = BeanUtil.copyProperties(role, DeviceDailyTaskVO.class);
        BeanUtil.copyProperties(account, userVO);
        BeanUtil.copyProperties(server, userVO);
        BeanUtil.copyProperties(sects, userVO);
        BeanUtil.copyProperties(deviceDailyTaskDTO, userVO);
        userVO.setDailyTasksId(dailyTasks.getId());

        // 将PO拷贝到VO
        return Result.isOk().data(userVO);
    }


    @Operation(summary = "提交完成的 每日任务id")
    @PutMapping("/{id}/over/{deviceId}/{temporary}")
    public Result updateDailyTask(
            @Parameter(description = "每日任务id") @PathVariable("id") Long id,
            @Parameter(description = "完成的设备") @PathVariable("deviceId") Long deviceId,
            @Parameter(description = "完成的设备临时窗口序列") @PathVariable("temporary") int temporary) {
        boolean dailyTaskOver = dailyTasksService.dailyTaskOver(id, deviceId, temporary);
        if (dailyTaskOver){
            DailyTasks dailyTasks = dailyTasksService.getById(id);
            dailyTasks.setStatus(1);
            dailyTasksService.updateById(dailyTasks);
            return Result.isOk();
        }
        else return Result.isFail();
    }

    @Operation(summary = "获取未完成的 每日任务")
    @GetMapping("/getDailyTask/{cpu}/{temporary}")
    public Result<DailyTasksVO> getDailyTask(
            @Parameter(description = "完成的设备") @PathVariable("cpu") String cpu,
            @Parameter(description = "完成的设备临时窗口序列") @PathVariable("temporary") int temporary) {
        Device device = deviceService.getDeviceByCard(cpu);
        if (device == null) {return Result.isFail(new Exception("设备没有记录"));}
        DailyTasks dailyTasks = dailyTasksService.selectByConditions(device.getId(),temporary);
        if (dailyTasks == null) {
            dailyTasks = dailyTasksService.queryTasksOnAccountUnUsd();
            if (dailyTasks == null) return Result.isOk().data(null);
            dailyTasksService.updateByConditions(dailyTasks.getId());
            deviceDailyTaskService.updateByConditions(dailyTasks.getId(),device.getId(),temporary);
        }

        Task task = taskService.getById(dailyTasks.getTaskId());
        DailyTasksVO dailyTasksVO = BeanUtil.copyProperties(dailyTasks, DailyTasksVO.class);
        dailyTasksVO.setTaskName(task.getName());
        return Result.isOk().data(dailyTasksVO);
    }

}
